/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mobsandgeeks.adapters.samples.slice;

import com.mobsandgeeks.adapters.samples.DynamicFragmentPagerAdapter;
import com.mobsandgeeks.adapters.samples.ResourceTable;
import com.mobsandgeeks.adapters.tab.DynamicPagerIndicator;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.PageSlider;

import java.util.ArrayList;
import java.util.List;

/**
 * 主页面
 *
 * @since 2021-06-30
 */
public class MainAbilitySlice extends AbilitySlice {
    private final int num200 = 200;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initView();
    }

    private void initView() {
        DynamicPagerIndicator dynamicPagerIndicator =
                (DynamicPagerIndicator) findComponentById(ResourceTable.Id_dynamic_pager_indicator);
        PageSlider pageSlider = (PageSlider) findComponentById(ResourceTable.Id_view_pager);
        pageSlider.setPageCacheSize(1);
        setViewPagerContent(pageSlider, dynamicPagerIndicator);
    }

    /**
     * 设置Tab适配器
     *
     * @param pageSlider pageSlider
     * @param dynamicPagerIndicator Indicator
     */
    private void setViewPagerContent(PageSlider pageSlider, DynamicPagerIndicator dynamicPagerIndicator) {
        List<Component> fragments = createFractions();
        DynamicFragmentPagerAdapter adapter =
                new DynamicFragmentPagerAdapter(fragments, (FractionAbility) getAbility());
        pageSlider.setProvider(adapter);
        pageSlider.setCurrentPage(1, false);
        dynamicPagerIndicator.setViewPager(pageSlider);
        getAbility().getUITaskDispatcher().delayDispatch(new Runnable() {
            @Override
            public void run() {
                pageSlider.setCurrentPage(0, false);
            }
        }, num200);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    /**
     * 创建fraction
     *
     * @return list
     */
    private List<Component> createFractions() {
        List<Component> fragments = new ArrayList<>();
        Component component = LayoutScatter.getInstance(getAbility()).parse(ResourceTable.Layout_item, null, false);
        fragments.add(component);
        Component component2 = LayoutScatter.getInstance(getAbility()).parse(ResourceTable.Layout_item2, null, false);
        fragments.add(component2);
        Component component3 = LayoutScatter.getInstance(getAbility()).parse(ResourceTable.Layout_item3, null, false);
        fragments.add(component3);
        return fragments;
    }
}
